// JavaScript Document
$(document).ready(function(){
						   function TotalRows(tableid)
							{
								var total=$(tableid+' tr').length;
								return total;
							}
						   $('#btnAddTopic').click(function(){
															 ResetModalRole();
														   $('#updateTopic h3').html('Tạo chuyên mục mới');
														    $('#task').val('Add');
															$('#topicID').val('');
															
															$('#cbxTopic option[value="'+$('#cbxTopic_Search option:selected').val()+'"]').attr('selected','selected');
														 
														   });
						    $('#tbResult a.update').live('click',function(){
																		  $('#updateTopic h3').html('Cập nhật chuyên mục');
																		   $('#task').val('Update');
															$('#topicID').val($(this).parent().parent().children().eq(1).text());

																		   ResetModalRole();
																		   $('#cbxTopic option[value="'+$('#cbxTopic_Search option:selected').val()+'"]').attr('selected','selected');
									$('#txtTopicName').val($(this).parent().parent().children().eq(2).text());
									$('#txtLink').val($(this).parent().parent().children().eq(3).text());
									$('#txtDescription').val($(this).parent().parent().children().eq(4).text());
									$('#chkIsCreateNews').attr('checked',$(this).parent().parent().children().eq(5).children('input[type="checkbox"]').eq(0).is(':checked'));
																		  });
							$('#cbxTopic_Search').change(function(){
									$parentID=$('#cbxTopic_Search option:selected').val();
									//alert($parentID);
									$.ajax({
										   url:'process/Topic_edit.php',
										   data:{task:"Search",parentID:$parentID},
										   type:'get',
										   dataType:'json',
										   cache:false,
										   success:function(res)
										   {
											$('#tbResult tr:not(:first)').each(function(i, option) { $(option).remove();});
											  for(var i=0;i<res.length;i++)
											   {
												   var check='';
											  if(res.iswrite)
											  {
												  check='checked=checked';
											  }
$html='<tr style="cursor:move;" id="'+(i+1)+'"><td class="order">'+res[i].order+'</td><td style="display:none">'+res[i].id+'</td><td>'+res[i].name+'</td><td>'+res[i]._link+'</td><td>'+res[i].desc+'</td><td><input type="checkbox" disabled="disable"'+check+'  /></td><td><a href="#" title="Xóa" class="delete"><img src="../Images/Icons/delete.png" alt="delete" /></a></td><td><a href="#updateTopic" data-toggle="modal" title="Sửa" class="update"><img src="../Images/Icons/page_edit.png" alt="update" /></a></td></tr>';
												 $('#tbResult tbody').append($html);
											 
											   }
											  
											 $("#tbResult").tableDnD({									
										onDrop: function(table, row) 
										{
											//alert('test');
											var index=0;
											$('#tbResult tbody tr td.order').each(function () {
											$(this).html(index);
											index++;
											});
										}									
									});
											  
										   }
										   
										   });
														   });
						
						   });
function ResetModalRole(){
	$('#txtTopicName').val('');
	$('#txtLink').val('');
	$('#txtDescription').val('');
	$('#cbxTopic option:selected').removeAttr('selected');
	$('#chkIsCreateNews').attr('checked','checked');
	}